import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.flink.api.common.functions.FilterFunction;
import org.apache.flink.api.common.serialization.SimpleStringSchema;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.connectors.kafka.FlinkKafkaConsumer;
import java.util.Properties;
import redis.clients.jedis.Jedis;

/**
 * @author : Ashiamd email: ashiamd@foxmail.com
 * @date : 2021/1/31 5:44 PM
 */
public class conka1 {

    public static JRedisUtil conn = new JRedisUtil("127.0.0.1",6379);
    public static void main(String[] args) throws Exception {
        // 创建执行环境

        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);
        // 设置并行度1
        Properties properties = new Properties();
        properties.setProperty("bootstrap.servers", "192.168.174.141:9092,192.168.174.142:9092,192.168.174.143:9092");
        // 下面这些次要参数
        properties.setProperty("group.id", "consumer-group");
        properties.setProperty("key.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
        properties.setProperty("value.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
        properties.setProperty("auto.offset.reset", "latest");

        // flink添加外部数据源
        DataStream<String> sourceStream = env.addSource(new FlinkKafkaConsumer<String>("test5", new SimpleStringSchema(),properties));
        DataStream<String> sourceStream1 = env.addSource(new FlinkKafkaConsumer<String>("test4", new SimpleStringSchema(),properties));
        sourceStream.map(message->{
            JSONObject jsonObj = JSON.parseObject(message);
            conn.incrBy("total",Float.parseFloat(jsonObj.getString("total").substring(0,jsonObj.getString("total").indexOf(".")+2)));
            conn.incrBy("order_sum",1);
            conn.sadd("rep",jsonObj.getString("recipient"));
            conn.zincrby("place_rank",1,jsonObj.getString("receiveplace").substring(0,2));
            return "数据成功输入到redis";
        });
        sourceStream1.map(message1->{
            JSONObject jsonObj1 = JSON.parseObject(message1);
            conn.zincrby("book_rank",Integer.parseInt(jsonObj1.getString("book_num")),jsonObj1.getString("book_name"));
            conn.zincrby("press_rank",Integer.parseInt(jsonObj1.getString("book_num")),jsonObj1.getString("book_press"));
            return "数据成功输入到redis";
        });

        env.execute();
    }
}